package Athena::Engine::Farm;

use strict;
use vars qw(@ISA);

use Athena::Connection;
use Athena::Engine;
use Exporter;

@ISA=qw(Athena::Engine);

sub new {
    # Get the class name
  my $stInvocant = shift(@_);
  my $stClass = ref($stInvocant) || $stInvocant;
  
  return $stClass->SUPER::new(@_);
}

sub refresh {
  my $fSelf = shift(@_);
  my $cConn = $fSelf->{connection};
  my $stSerials = $cConn->httpGet($cConn->getUrl() . '/status/farm/?n=*,f=%s');
  my @arSerials = split(/\n/, $stSerials);
  my $stSerial;
  foreach $stSerial (@arSerials) {
    $fSelf->_deserialize($stSerial);
  }
}

sub update {
  my $pSelf = shift(@_);
  my $cConn = $pSelf->{connection};
  my $pFarm;
  my $stUrl;
  my $stResult;
  foreach $pFarm (keys(%{$pSelf->{table}})) {
    $stUrl = $cConn->getUrl()
      . '/update/farm/?'
	. "n=" . $pSelf->{table}->{$pFarm}->{name}
	  . ",o=" . $pSelf->{table}->{$pFarm}->{on};
    $stResult = $cConn->httpGet($stUrl);
    if (($stResult) && ($stResult ne "\n")) {
      print STDERR "athena.pl: error updating farm: $stUrl\n";
    }
  }
}
  
sub _deserialize {
  my $fSelf = shift(@_);
  my $stSerial = shift(@_);
  # my @arSerial = split(/\|/, $stSerial);
  my ($idx, $exists, $name, $downurl, $offurl, $algodef, $hitadds, $hitaddsto,
      $on, $expect_update, $allow_ttl, $recover_ttl, $member_chosen_idx, 
      $phys_idx_list) =  split(/\|/, $stSerial);
  $fSelf->{table}->{$idx}->{exists} = $exists;
  $fSelf->{table}->{$idx}->{name} = $name;
  $fSelf->{table}->{$idx}->{downurl} = $downurl;
  $fSelf->{table}->{$idx}->{offurl} = $offurl;
  $fSelf->{table}->{$idx}->{algodef} = $algodef;
  $fSelf->{table}->{$idx}->{hitadds} = $hitadds;
  $fSelf->{table}->{$idx}->{hitaddsto} = $hitaddsto;
  $fSelf->{table}->{$idx}->{on} = $on;
  $fSelf->{table}->{$idx}->{expect} = $expect_update;
  $fSelf->{table}->{$idx}->{allow} = $allow_ttl;
  $fSelf->{table}->{$idx}->{recover} = $recover_ttl;
  $fSelf->{table}->{$idx}->{chosen} = $member_chosen_idx;
  $fSelf->{table}->{$idx}->{members} = $phys_idx_list;
#   my @phys_members = split(",", $phys_idx_list);
#   my $key;
#   foreach $key (@phys_members) {
#     $fSelf->{table}->{$idx}->{member_phys_idx}->{$key} = 1;
#   }
  return $idx;
}


1;
